<?php


namespace app\admins\controller;


use app\BaseController;
use app\Request;
use think\facade\Db;

class Category extends BaseController
{
    protected $middleware = [\app\middleware\Check::class];
    public function index(){
        return view();
    }

    //    分页展示
    public function article(Request $request){

//        查询条件
        $where = [];
        if (!empty($request->param('name'))) {
            $where[] = ['category_name|category_id', 'like', "%{$request->param('name')}%"];
        }
        //获取每页显示的条数
        $limit= $request->param('limit');
        //获取当前页数
        $page= $request->param('page');
        $list = Db::name('category')
            ->where($where)
            ->paginate($limit,false,['page'=>$page])
            ->toArray();
        return json(['code'=>0,'msg'=>'','count'=>$list['total'],'data'=>$list['data']]);

    }

    //    添加分类
    public function addBook(){
        $data = request()->param();
//        return $data;
        $data1 = [
            'category_name' => $data['bname'],
            'create_at' => $data['create_at']
        ];

        $Db = Db::name('category');

        if($Db->where('category_name',$data['bname'])->find()) {
            return  json(["state"=>204,"message"=>"分类名不能重复"]);
        }

        $ret = $Db->insert($data1);

        if($ret){
            return  json(["state"=>200]);
        }
        else{
            return json(["state"=>204,"message"=>'添加分类失败']);
        }


    }

    //    删除分类
    public function delete()
    {
        if(request()->isGet()){
            $data = input('get.');
            Db::name('category')->where('category_id',$data['id'])->delete();
        }

    }

}